package com.mfp.java.testYou.core;

import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import com.mfp.java.testYou.core.utils.Utils;
import com.mfp.java.testYou.model.Language;

public class LanguagesFactory {

	private static final Logger log = Logger.getLogger(LanguagesFactory.class);
	
	private static final String DEF_LANGS = "en,ro";
	
	private static final String I18N_PATTERN = "lang.%s";
	//private static final String IMG_PATTERN = "images/flags/%s.png";
	private static final String IMG_PATTERN = "/testYou/resources/images/flags_%s.png";

	private LanguagesFactory() {
	}

	public static List<Language> createList() {
		List<Language> langs = new LinkedList<Language>();
		String langsString = Utils.readConfig(AppContext.CONFIG_FILE, "app.langs");
		if (langsString == null || langsString.length() == 0) {
			log.warn("No language config found, using default");
			langsString = DEF_LANGS;
		}
		String[] langsArray = langsString.split(",");
		for (String langString : langsArray) {
			
			String i18n = String.format(I18N_PATTERN, langString);
			String img = String.format(IMG_PATTERN, langString);
			Language l = new Language(langString, i18n, img);
			
			langs.add(l);
		}
		return langs;
	}
}
